<template>
  <div class="toggle">
    <div>
      <div class="word">选择类型</div>
      <div class="select" style="width: 160px;">
        <el-select v-model="type" placeholder="请选择你所需要的代码类型">
          <el-option v-for="(x, y) in codeList" :key="y" :value="x.value" :label="x.label">
          </el-option>
        </el-select>
      </div>
      <div class="btn"><el-button @click="toggle">转化</el-button></div>
      <div class="btn"><el-button @click="copy">复制</el-button></div>
      <div class="btn"><el-button @click="clear">清空</el-button></div>
    </div>
    <div>
      <div class="word">输入代码</div>
      <div class="text"><el-input v-model="code" type="textarea" :rows="10"></el-input></div>
    </div>
    <div>
      <div class="word">转化代码</div>
      <div class="text"><el-input v-model="result" type="textarea" :rows="10"></el-input></div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import element from '@/utils/element';
const md5 = require('js-md5')
const code = ref('')
const result = ref('')
const type = ref(0)
const selectList = [
  'function 转 const',
  'java 转 obj',
  'eltable 转 const',
  'returndata 转 constRef',
  'excel 转 obj',
  'md5加密',
  '代码拆解',
  '数字大写'
]
const codeList = []
selectList.map((x, y) => {
  codeList.push({
    label: x,
    value: y
  })
})

const functionToConst = (code) => {
  return code
    .replace(/function/g, "const")
    .replace(/\(/g, " = (")
    .replace(/\)/g, ") =>")
}

const javaToObj = (code) => {
  return code
    .replace(/@Excel\(name = /g, "{\ntitle:")
    .replace(/private String /g, "name: \"")
    .replace(/\)/g, ",")
    .replace(/;/g, "\",\nwidth: 180\n},");
}

const tableToObj = (code) => {
  return code
    .replace(/<el-table-column label=/g, "{\ntitle:")
    .replace(/ align="center" prop=/g, ",\nname: ")
    .replace(/\/>/g, ",\nwidth: 80\n},");
}

const returndataToconstRef = (code) => {
  return code
    .replace(/return {/g, 'const')
    .replace(/}/g, ');')
    .replace(/:/g, '= ref(')
    .replace(/,/g, ');\nconst ')
    .replace(/const \n/g, 'const ')
}

const excelToObj = (code) => {
  let arr = code.split('\n')
  let columns = arr[0].split('\t')
  let list = []
  function arrToObj(col, sz) {
    let obj = {}
    col.map((x, y) => {
      if (sz.length > 1) {
        obj[x] = sz[y]
      }
    })
    return obj
  }

  arr.map((x, y) => {
    if (y > 0 && x != '') {
      list.push(
        arrToObj(columns, x.split('\t'))
      )
    }
  })
  // console.log(list)
  let str = JSON.stringify(list)
    .replace(/},/g, '},\n')
    .replace(/\[/g, '\[\n')
    .replace(/\]/g, '\n\]')
    .replace(/{/g, '\t{')
    .replace(/{"/g, '{')
    .replace(/":/g, ':')
    .replace(/,"/g, ',')
  return str
}

const getFirstPart = (code) => {
  let arr = code.split('\n')
  let arr1 = []
  let arr2 = []
  arr.map(x => {
    arr1.push(x.split('：')[0])
    arr2.push(x.split('：')[1])
  })
  return (arr1 + '\n' + arr2).replace(/,/g, '\n').replace(/"/, '')
}

function ToString(n) {
  if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n)) {
    return "数据非法";  //判断数据是否大于0
  }
  var unit = "千百拾亿千百拾万千百拾元角分", str = "";
  n += "00";
  var indexpoint = n.indexOf('.');  // 如果是小数，截取小数点前面的位数
  if (indexpoint >= 0) {
    n = n.substring(0, indexpoint) + n.substr(indexpoint + 1, 2);   // 若为小数，截取需要使用的unit单位
  }
  unit = unit.substr(unit.length - n.length);  // 若为整数，截取需要使用的unit单位
  for (var i = 0; i < n.length; i++) {
    str += "零壹贰叁肆伍陆柒捌玖".charAt(n.charAt(i)) + unit.charAt(i);  //遍历转化为大写的数字
  }
  return str.replace(/零(千|百|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1")
  .replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整"); // 替换掉数字里面的零字符，得到结果
}


const toggle = () => {
  switch (type.value) {
    case 0:
      result.value = functionToConst(code.value)
      break;
    case 1:
      result.value = javaToObj(code.value)
      break;
    case 2:
      result.value = tableToObj(code.value)
      break;
    case 3:
      result.value = returndataToconstRef(code.value)
      break;
    case 4:
      result.value = excelToObj(code.value)
      break;
    case 5:
      result.value = md5(code.value)
      break;
    case 6:
      result.value = getFirstPart(code.value)
      break;
    case 7:
      result.value = ToString(code.value)
      break;
  }
}

const copy = () => {
  navigator.clipboard.writeText(result.value).then(() => {
    element.success('复制成功')
  })
}

const clear = () => {
  code.value = ''
  result.value = ''
}
</script>

<style scoped lang="scss">
.toggle {
  >div {
    display: flex;
    margin-bottom: 10px;

    .word {
      width: 100px;
      color: #ccc;
      display: flex;
      font-size: 14px;
      justify-content: center;
      align-items: center;
    }

    .input {
      width: 600px;
    }

    .text {
      width: 600px;
    }

    .btn {
      width: 60px;
      margin-left: 10px;
    }
  }
}
</style>
